/*
 * @Author: your name
 * @Date: 2019-12-03 21:14:01
 * @LastEditTime: 2019-12-09 14:54:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \pc\src\pages\OperatingPlan\ContractPlan\VersionDetail\models\items.js
 */
import _ from 'lodash';
import * as api from '@/services/ContractPlanMgr';
import * as utils from '../utils/treeUtils';

const Model = {
  namespace: 'ContractPlanItems',
  state: {
    all: [],
    searched: [],
    overAllYears: [],
    expandedRowKeys: [],
    searchItems: {},
    currentYear: '',
    currentMonths: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'],
  },
  effects: {
    *init({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: {
          all: [],
          searched: [],
          overAllYears: [],
          expandedRowKeys: [],
          searchItems: {},
        },
      });

      let currentYear = yield select(state => state.ContractPlanItems.currentYear);
      currentYear = currentYear || '';
      const params = { ...payload.params, year: currentYear };
      const response = yield call(api.getSignPlanVersionUsingGET, { params });
      if (
        response &&
        response.data &&
        !_.isEmpty(response.data) &&
        !_.isEmpty(response.data.detailList)
      ) {
        const all = response.data.detailList;
        const overAllYears = response.data.timeList;
        if (currentYear === '') {
          if (_.includes(overAllYears, new Date().getFullYear().toString())) {
            currentYear = new Date().getFullYear().toString();
          } else {
            currentYear = _.first(overAllYears);
          }
        }

        const searchItems = utils.getSearchItems(all);
        // 获取展开的列key
        const expandedRowKeys = utils.getExpandedRowKeys(all, 'signDetailId');
        const searched = utils.buildTree(all);

        yield put({
          type: 'save',
          payload: { all, overAllYears, searched, expandedRowKeys, searchItems, currentYear },
        });
      }
      if (callback) callback(response && response.data ? response.data : []);
    },

    *changeCurrentYear({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { currentYear: payload.currentYear },
      });
      if (callback) callback();
    },

    *approval({ payload, callback }, { call, put }) {
      const response = yield call(api.approvedCheckUsingGET_1, payload);
      if (response && response.data) {
        if (response) callback();
      }
    },
  },
  reducers: {
    changeCurrentMonths(state, action) {
      let currentMonths = action.payload.currentMonths;
      if (currentMonths == null || currentMonths.length === 0) {
        currentMonths = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'];
      }
      return {
        ...state,
        currentMonths: currentMonths,
      };
    },
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    clear() {
      return {
        all: [],
        searched: [],
        overAllYears: [],
        expandedRowKeys: [],
        searchItems: {},
        currentYear: '',
        currentMonths: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14'],
      };
    },
  },
};
export default Model;
